package com.qf.b_servlet;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.*;
import java.util.List;

/**
 * @author zdl
 * @date 2025/1/13
 */
public class Demo2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
       /* Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1_2?useSSL=false", "root", "123456");
        String sql = "select * from tb_user where username = ? and password = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,"user");
        ps.setString(2,"123");
        ResultSet resultSet = ps.executeQuery();
        boolean next = resultSet.next();
        if (next){
            System.out.println("登录成功");
        }else {
            System.out.println("失败");
        }*/
        /*List<User> users = findAll();
        for (User user : users) {
            System.out.println(user);
        }*/
        dbUtilQuery();
    }
    public static List<User> findAll() throws SQLException {
        QueryRunner qr = new QueryRunner();
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1_2?useSSL=false", "root", "123456");
        String sql = "select id,username,password,phone,create_time as createTime,money,sex from tb_user";
        List<User> userList = qr.query(conn, sql, new BeanListHandler<>(User.class));
        return userList;
    }
    public static void dbUtilQuery() throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1_2?useSSL=false", "root", "123456");
        String sql = "select * from tb_user where username = ? and password = ?";
        QueryRunner queryRunner = new QueryRunner();
        Object[] params = {"user","123"};
        User query = queryRunner.query(conn, sql,params,new BeanHandler<>(User.class));
        if (query != null){
            System.out.println(query);
            System.out.println("成功");
        }else {
            System.out.println("失败");
        }
    }
}
